package com.xbdun.comweb.bmp;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import com.xbdun.common.util.SpringUtil;
import com.xbdun.comweb.bmp.freemark.AuthBtnDirective;
import com.xbdun.comweb.bmp.freemark.FreeMarkerViewUtil;
import com.xbdun.comweb.bmp.freemark.UploadFileDirective;
import com.xbdun.comweb.bmp.interceptor.StringToDateConverter;
import com.xbdun.comweb.bmp.interceptor.UserInterceptor;
/**
 * 所有Web都要继承该父类
 * @author zhanggt
 */
@ComponentScan(basePackageClasses=BaseApplicationRunner.class)
//@PropertySource(value="classpath:/${spring.application.name}/${spring.config.name}-${spring.profiles.active}.properties")
@ImportResource("classpath:/spring/spring-root.xml")
public abstract class BaseApplicationRunner extends WebMvcConfigurerAdapter implements CommandLineRunner,ApplicationContextAware{
	@Autowired  
    FreeMarkerConfigurer freeMarkerConfigurer;
	@Autowired  
	AuthBtnDirective authBtn;//授权按钮
	@Autowired
	UploadFileDirective uploadFileDirective;//文件上传
	@Autowired
	FreeMarkerViewResolver freeMarkerViewResolver;
	@Autowired  
	UserInterceptor userInterceptor;
	@Autowired
	StringRedisTemplate stringRedisTemplate;
	@Autowired
    RequestMappingHandlerAdapter handlerAdapter;//时间转换器
	@Value("${xbdun.static.folder.html}")
	private String xbdunDir="";//储存生成的静态文件
	private static String XBDUN_DIR = null;
	@Override
	public void run(String... args) throws Exception {
		freeMarkerViewResolver.setViewClass(FreeMarkerViewUtil.class);
		if(null==XBDUN_DIR||(!xbdunDir.equals(XBDUN_DIR)))XBDUN_DIR = xbdunDir;
		xbdunDir = null;
		//Controller对应的时间转换器
		ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter.getWebBindingInitializer();
        if (initializer.getConversionService() != null) {
            GenericConversionService genericConversionService = (GenericConversionService) initializer.getConversionService();
            genericConversionService.addConverter(new StringToDateConverter());
        }
      //设置freemark权限按钮
      freeMarkerConfigurer.getConfiguration().setSharedVariable("AuthBtn", authBtn);
      //设置上传文件
      freeMarkerConfigurer.getConfiguration().setSharedVariable("UploadFile", uploadFileDirective);
	}
	/**获取配置静态资源额外路径**/
	public final static String getXbdunStaticDir(){
		return XBDUN_DIR;
	}
	@Override  
    public void addInterceptors(InterceptorRegistry registry) {
       //注册监控拦截器  
       registry.addInterceptor(userInterceptor).addPathPatterns("/**").excludePathPatterns("/html/**/*.html");  
    }
	//作废，可以还原重写@Override  
    public void addCorsMappings1(CorsRegistry registry) {
        registry.addMapping("/**")
        		.allowedOrigins("*")
        		.allowedHeaders("*/*")
        		.allowedMethods("*")
        		.maxAge(120);
    }
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		SpringUtil.setApplicationContext(applicationContext);
	}
	
	//权限按钮
	@Bean
	public AuthBtnDirective generoAutBtn(){
		return new AuthBtnDirective();
	}
	
	//文件上传控件
	@Bean
	public UploadFileDirective generoUploadFile(){
		return new UploadFileDirective();
	}
}
